package com.vrv.app.cyberspace.modules.sys.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.vrv.app.cyberspace.common.ddio.ApiV2Service;
import com.vrv.app.cyberspace.common.ddio.DDIORunnable;
import com.vrv.app.cyberspace.common.ddio.TemplateMessage;
import com.vrv.app.cyberspace.common.utils.HttpContextUtils;
import com.vrv.app.cyberspace.common.utils.JSONResult;
import com.vrv.app.cyberspace.config.WXBSystemConfig;
import com.vrv.app.cyberspace.modules.sys.entity.BulletinApprovalTable;
import com.vrv.app.cyberspace.modules.sys.entity.TodayBulletinTable;
import com.vrv.app.cyberspace.modules.sys.entity.UserTable;
import com.vrv.app.cyberspace.modules.sys.mapper.BulletinApprovalTableMapper;
import com.vrv.app.cyberspace.modules.sys.mapper.TodayBulletinTableMapper;
import com.vrv.app.cyberspace.modules.sys.service.IBulletinApprovalTableService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.vrv.app.cyberspace.common.utils.JSONResult.renderError;
import static com.vrv.app.cyberspace.common.utils.JSONResult.renderSuccess;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sm
 * @since 2017-12-15
 */
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Service
@Transactional
public class BulletinApprovalTableServiceImpl extends ServiceImpl<BulletinApprovalTableMapper, BulletinApprovalTable> implements IBulletinApprovalTableService {
    Logger logger = LoggerFactory.getLogger(RiskTableServiceImpl.class);

    private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    @Autowired
    private TodayBulletinTableMapper todayBulletinTableMapper;
    @Autowired
    private ApiV2Service apiV2Service;
    @Autowired
    private WXBSystemConfig wxbSystemConfig;
    private ExecutorService executorService = Executors.newSingleThreadExecutor();

    @Override
    public JSONResult endTodayBulletinApproval(Integer id, String advice) {
        UserTable userTable = (UserTable) HttpContextUtils.getHttpServletRequest().getSession().getAttribute("user");
        TodayBulletinTable tbt = todayBulletinTableMapper.selectById(id);
        if (tbt.getStatus()==3){
            return renderError("此今日快报审批已完结!");
        }
        if (!tbt.getApprovalUserID().equals(userTable.getLinkdoodID())){
            return renderError("操作被禁止，当前用户非审批人！");
        }
        //新增审批流程状态5领导同意
        Date now = new Date();
        BulletinApprovalTable bat = new BulletinApprovalTable();
        bat.setTodayBulletinID(id);
        bat.setUserID(tbt.getApprovalUserID());
        bat.setStatus(5);
        bat.setCreateTime(now);
        bat.setAdvice(advice);
        bat.insert();
        //更改今日快报状态为3办结（审批完成）
        tbt.setStatus(3);
        tbt.setUpdateTime(now);
        tbt.updateById();
        logger.info("审批今日快报"+tbt.getId()+"由"+userTable.getLinkdoodID()+"-"+userTable.getLinkdoodName()+"审批同意");
        return renderSuccess();
    }

    @Override
    public JSONResult backTodayBulletinApproval(Integer id, String advice) {
        UserTable userTable = (UserTable) HttpContextUtils.getHttpServletRequest().getSession().getAttribute("user");
        if (userTable==null){
            return renderError("操作被禁止，会话超时，重启豆豆浏览器！");
        }
        TodayBulletinTable tbt = todayBulletinTableMapper.selectById(id);
        if (tbt.getStatus()==3){
            return renderError("此今日快报审批已完结!");
        }
        if (!tbt.getApprovalUserID().equals(userTable.getLinkdoodID())){
            return renderError("操作被禁止，当前用户非审批人！");
        }
        //新增审批流程状态2领导退回
        Date now = new Date();
        BulletinApprovalTable newbat1 = new BulletinApprovalTable();
        newbat1.setTodayBulletinID(id);
        newbat1.setUserID(tbt.getApprovalUserID());
        newbat1.setStatus(2);
        newbat1.setCreateTime(now);
        newbat1.setAdvice(advice);
        newbat1.insert();
/*        //新增审批流程状态3值班员审核中
        BulletinApprovalTable newbat2 = new BulletinApprovalTable();
        newbat2.setTodayBulletinID(id);
        newbat2.setUserID(tbt.getCreateUserID());
        newbat2.setStatus(3);
        newbat2.setCreateTime(now);
        newbat1.setAdvice("值班员审核中");
        newbat2.insert();*/
        //更改今日快报状态1，值班员审批
        tbt.setStatus(1);
        tbt.setApprovalUserID(tbt.getCreateUserID());
        tbt.setUpdateTime(now);
        tbt.updateById();
        logger.info("审批今日快报"+tbt.getId()+"由"+userTable.getLinkdoodID()+"-"+userTable.getLinkdoodName()+"审批退回");
        pushTemplateMessage(id);
        return renderSuccess();
    }

    @Override
    public JSONResult addTodayBulletinApproval(Integer id, String advice, String approvalUserID) {
        UserTable userTable = (UserTable) HttpContextUtils.getHttpServletRequest().getSession().getAttribute("user");
        TodayBulletinTable tbt = todayBulletinTableMapper.selectById(id);
        if (tbt.getStatus()==3){
            return renderError("此今日快报审批已完结!");
        }
        if (!tbt.getApprovalUserID().equals(userTable.getLinkdoodID())){
            return renderError("操作被禁止，当前用户非审批人！");
        }
        //新增审批流程状态，为4请示上级领导中
        Date now = new Date();
        BulletinApprovalTable newbat1 = new BulletinApprovalTable();
        newbat1.setTodayBulletinID(id);
        newbat1.setUserID(tbt.getApprovalUserID());
        newbat1.setStatus(4);
        newbat1.setCreateTime(now);
        newbat1.setAdvice(advice);
        newbat1.insert();
/*        //新增审批流程状态1领导审批中
        BulletinApprovalTable newbat2 = new BulletinApprovalTable();
        newbat2.setTodayBulletinID(id);
        newbat2.setUserID(approvalUserID);
        newbat2.setStatus(1);
        newbat2.setCreateTime(now);
        newbat1.setAdvice("领导审批中");
        newbat2.insert();*/
        //更改今日快报状态2，领导审批
        tbt.setStatus(2);
        tbt.setApprovalUserID(approvalUserID);
        tbt.setUpdateTime(now);
        tbt.updateById();
        logger.info("审批今日快报"+tbt.getId()+"由"+userTable.getLinkdoodID()+"-"+userTable.getLinkdoodName()+"转批，转批人为："+ approvalUserID);
        pushTemplateMessage(id);
        return renderSuccess();
    }

    private void pushTemplateMessage(Integer id) {
        TodayBulletinTable tbt = todayBulletinTableMapper.selectById(id);
        String appid = apiV2Service.getAppID();
        String content = "时间: " + dateFormat.format(new Date()) + "\n标题：" + tbt.getName() + "\n类别: " + "今日快报" + "\n请您及时处理 ";
        TemplateMessage templateMessage = TemplateMessage.createOne(appid, "今日快报", appid, content, wxbSystemConfig.getTodayBulletinDetailUrl() + "&id=" + tbt.getId(),
                tbt.getApprovalUserID());
        try {
            executorService.execute(new DDIORunnable(apiV2Service, "sendTemplateMsg", templateMessage, TemplateMessage.class));
        } catch (NoSuchMethodException e) {
            logger.info("推送今日快报(ID" + tbt.getId() + ")消息失败! ");
            e.printStackTrace();
        }
    }
}
